package airthmetic.exercise.sort.cmp;


// 插入排序 AC   有时未AC 超时
public class _912_排序数组_Insert {
    public int[] sortArray(int[] nums) {
        if(nums.length < 2){
            return nums;
        }
        int len = nums.length;

        int current,pos;
        for(int i=1; i<len; i++){
            pos = i-1;
            current = nums[i];
            while (pos >=0 && nums[pos] > current){
                nums[pos + 1] = nums[pos];
                pos--;
            }

            nums[pos + 1] = current;
        }


        return nums;
    }

    public static void main(String[] args) {
        _912_排序数组_Insert insert = new _912_排序数组_Insert();
        int[] nums ={5,2,3,1};
        int[] ints = insert.sortArray(nums);
        for (int i : ints){
            System.out.println(i);
        }
    }

    public void swap(int[] nums, int i, int j){
        if(i == j){
            return;
        }
        nums[i] ^= nums[j];
        nums[j] ^= nums[i];
        nums[i] ^= nums[j];
    }
}
